package com.dcdata.xian;

import com.alibaba.fastjson.JSON;
import com.dcdata.xian.pojo.GjznCard;
import com.dcdata.xian.pojo.GjznRoute;
import com.dcdata.xian.pojo.GjznStation;
import com.dcdata.xian.pojo.LineExtension;
import com.dcdata.xian.utils.BeanUtil;
import lombok.extern.java.Log;
import oracle.jdbc.replay.OracleDataSource;
import oracle.jdbc.replay.OracleDataSourceImpl;

import java.io.*;
import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.ParseException;
import java.time.LocalDate;
import java.util.Calendar;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

/**
 * @author: xinmi
 * @date: 2020/11/19
 * @description:
 */
@Log
public class MysqlInsert2 {

    private MysqlOperationDg mysqlOperationDg;

    public static void main(String[] args) throws SQLException, ParseException {

        MysqlInsert2 mysqlInsert = new MysqlInsert2();
        mysqlInsert.init();
        mysqlInsert.run();
    }

    void init(){
        mysqlOperationDg = new MysqlOperationDg();
        mysqlOperationDg.connectToDatabase();
    }

    void run() throws ParseException {
        lineTable();

    }

    private void lineTable() {
        LineExtension lineExtension = new LineExtension(1l,10,11,"1",1,5);

        LineExtension qResult = mysqlOperationDg.getLineExtensionMapper().getLineExtensionByIdAndVersion(lineExtension.getLineId(), lineExtension.getLineVersion());

        if (qResult != null) {
            try {
                if (BeanUtil.lineExtensionDataSameCheck(qResult, lineExtension)) {
                    log.info("lineExtension already exist and item not changed!");
                } else {
                    log.info("lineExtension already exist and item changed, should update !");
                    mysqlOperationDg.getLineExtensionMapper().updateLineExtensionByIdAndVersion(lineExtension);
                    /*mysqlOperationDg.getLineExtensionMapper().updateLineExtensionItemByIdAndVersion(lineExtension.getLineId(),
                            lineExtension.getLineVersion(),
                            lineExtension.getRouteSystemType(),
                            lineExtension.getAreaId(),
                            lineExtension.getVehiclesCount());*/
                    mysqlOperationDg.getSqlSession().commit();

                }
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        } else {
            log.info("ready for save lineExtension ");
            mysqlOperationDg.insertLineExtension(lineExtension);
            log.info("save lineExtension succeeded.");
        }

    }



    private static void show(String msg) {
        System.out.println(msg);
    }

    static void showError(String msg, Throwable exc) {
        System.out.println(msg + " hit error: " + exc.getMessage());
    }
}
